package byj.com.meishi.Ansyc;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

import java.io.IOException;

import byj.com.meishi.cache.BitmapCache;
import byj.com.meishi.utils.HttpUtil;

public class LoadImgTask extends AsyncTask<String, Void, Bitmap> {

	// private ImageView mIvLogo;

	// public LoadImgTask(ImageView iv) {
	// this.mIvLogo = iv;
	// }

	public interface OnGetImageResultListener {
		void onGetImageResult(Bitmap result);
	}

	private OnGetImageResultListener mListener;

	public LoadImgTask(OnGetImageResultListener listener) {
		this.mListener = listener;
	}

	@Override
	protected Bitmap doInBackground(String... params) {
		try {
			byte[] data = HttpUtil.loadData(params[0]);
			if(data!=null){
				Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
				if(bitmap!=null){
					BitmapCache.getInstance().put(params[0],bitmap);
					return bitmap;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onPostExecute(Bitmap result) {
		super.onPostExecute(result);
		if (result != null&&mListener!=null) {
			//mIvLogo.setImageBitmap(result);
			mListener.onGetImageResult(result);
		}
	}

}
